#!/bin/sh

#
# Test the GPT creation ability of fwup but with a partial 128KB segment at end
#
# This is the same as 163_gpt.test except with a search/replace of 65536 with
# 65504.
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
define(EFI_TYPE, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")
define(LINUX_TYPE, "44479540-f297-41b2-9af7-d131d5f0458a")

define(DISK_UUID, "b443fbeb-2c93-481b-88b3-0ecb0aeba911")
define(EFI_PART_UUID, "5278721d-0089-4768-85df-b8f1b97e6684")
define(ROOTFS_PART_UUID, "fcc205c8-2f1c-4dcd-bef4-7b209aa15cca")

define(EFI_PART_OFFSET, 64)
define(EFI_PART_COUNT, 32768)
define-eval(ROOTFS_PART_OFFSET, "\${EFI_PART_OFFSET} + \${EFI_PART_COUNT}")
define(ROOTFS_PART_COUNT, 65536)

gpt gpt-a {
    guid = \${DISK_UUID}

    partition 0 {
        block-offset = \${EFI_PART_OFFSET}
        block-count = \${EFI_PART_COUNT}
        type = \${EFI_TYPE}
        guid = \${EFI_PART_UUID}
        name = "efi-part.vfat"
    }
    partition 1 {
        block-offset = \${ROOTFS_PART_OFFSET}
        block-count = \${ROOTFS_PART_COUNT}
        type = \${LINUX_TYPE}
        guid = \${ROOTFS_PART_UUID}
        name = "rootfs.ext2"
    }
}
task complete {
	on-init {
                gpt_write(gpt-a)
        }
}
EOF

# Create the expected output from sfdisk by running ./198_gpt_partial-generate.sh on
# Linux.
base64_decodez >$WORK/expected-primary-gpt.img <<EOF
H4sIAAAAAAAAA+3SzyuDcRzA8c+z2sVB7WyynJBNc1U8w4y00uJkBztscaJno5XSoyhcJbUTR444
2A6Kk/mRKGd/wEpIUdTjq77PYcfHQdT79e1bn8/316dPfUXwn/nk0XEcQ0XLtuH59sR+fGgkNBZL
jYsYklYrV+3R+e8d9y331VadT+m89jFwtNnZNLx22HjZUNsL+PS+refNZLD/Zx3hN7XlO05b3u8C
lVHZqfZWe5qtYmpdphOrD8fP5aXciqnP9dn197KSkxkJy5xkxJKCRGRBrWRU5I25lRjcfjmIld5u
o/dP8Y1z/9lnsOs6efK6GCrtpi9M261f/7stmVWjoGrmVe2sFFXc7b19AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAPgzvgCgS9s5AEIAAA==
EOF
base64_decodez >$WORK/expected-secondary-gpt.img <<EOF
H4sIAAAAAAAAA+3bsSvEYRzH8c8NFoO62V0uk+SIVfE7nHNJ6bK5wW+4i4l+d+mi9FMU1iN1E6MV
AwbF5EiilM0fcAtSFINHnhtuPIMM79fT0/N9vvX06fkDvm259rOW97vgyah2yn3l3rBXSK1rOrH6
ePR8vJRdcfSj31eNjLKaUVRzcuUpr07Nm45rqvo4W4mh7Zf9WOnttvv+Kb5x0XD+Geq6Hjt9XYyU
dtOXjl/ND9S88zRrVt5k5kx2RgVT99SZDQAAAAAAAAAAAAAAAAAAAAAAAAAAAADAfxEfTkbGY6kJ
KaC0uW8uPIS/+8t23r46dd9qzynbr3wMHhY7mkfWDpquGit7Qdf2fbtvJkMDf/UH/N4XQ6EbhABC
AAA=
EOF
cp $WORK/expected-primary-gpt.img $WORK/expected.img
dd if=$WORK/expected-secondary-gpt.img of=$WORK/expected.img bs=512 seek=98401 conv=notrunc

# Create the firmware file, then "burn it"
$FWUP_CREATE -c -f $CONFIG -o $FWFILE
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete --max-size=98434

cmp $WORK/expected.img $IMGFILE

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
